package com.cheung.leetcode.arr;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.arr
 * @Author :  cheungming
 * @CreateTime : 2024-07-28 11:03:06
 * @Description : 42 . 接雨水
 * @Version : 1.0
 */
public class Code42Test {

    public int trap(int[] height) {
        int ans = 0;
        for (int i = 0; i < height.length; i++) {
            // 当前柱子左边最大值
            int leftMax = 0;
            for (int j = i; j >= 0; j--) {
                leftMax = Math.max(leftMax, height[j]);
            }

            // 当前柱子右边最大值
            int rightMax = 0;
            for (int j = i; j < height.length; j++) {
                rightMax = Math.max(rightMax, height[j]);
            }

            // 减去当前柱子的高度
            ans += Math.min(leftMax, rightMax) - height[i];
        }

        return ans;
    }

    @Test
    public void test1() {
        assert trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}) == 6;
    }

    @Test
    public void test2() {
        assert trap(new int[]{4, 2, 0, 3, 2, 5}) == 9;
    }

}
